package me.ahoukuse.koala.config;

import me.ahoukuse.koala.interceptor.UserValidateInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserValidateInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/file/getDownLoadToken","/file/downloadByToken", "/user/signup", "/login", "/getCaptcha","/file/getSharedFiles", "/file/download", "/share/getSharedBySharedCode");
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // TODO REMOVE WHEN RELEASE
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .maxAge(3600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
    }
}
